package nccloud.web.ic.custrecloss.action.transfer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.ic.m4c.entity.SaleOutBodyVO;
import nc.vo.ic.m4c.entity.SaleOutVO;
import nc.vo.ic.pub.util.StringUtil;
import nc.vo.ic.pub.util.ValueCheckUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.scmpub.res.billtype.ICBillType;
import nc.vo.scmpub.util.BillBodySortUtils;
import nccloud.base.exception.ExceptionUtils;
import nccloud.dto.scmpub.page.entity.SCMQueryTreeFormatVO;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.ClientInfo;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.model.row.Cell;
import nccloud.framework.web.ui.pattern.billgrid.BillGrid;
import nccloud.framework.web.ui.pattern.billgrid.BillGridOperator;
import nccloud.nc.vo.pub.pf.PullBillInfoVO;
import nccloud.pubitf.ic.custrecloss.service.IWastAgeFetchBillService;
import nccloud.pubitf.platform.query.INCCloudQueryService;
import nccloud.pubitf.riart.businesstype.IPFBusinessTypeService;
import nccloud.web.ic.outbound.saleout.action.SaleOutBillGridPrecisionHandler;
import nccloud.web.ic.pub.precision.ICBaseBillGridPrecisionHandler;



public class WastAgeQrySaleOutAction
  implements ICommonAction
{
  public Object doAction(IRequest request) {
    String read = request.read();
    IJson json = JsonFactory.create();
    
    SCMQueryTreeFormatVO info = (SCMQueryTreeFormatVO)json.fromJson(read, SCMQueryTreeFormatVO.class);
    INCCloudQueryService queryutil = (INCCloudQueryService)ServiceLocator.find(INCCloudQueryService.class);
    IQueryScheme scheme = queryutil.convertCondition(info.getQueryInfo());
    Object transtype = null;
    PullBillInfoVO[] refbillinfos = queryRefPara(transtype);
    SaleOutVO[] vos = null;
    
    if (refbillinfos == null || refbillinfos.length < 1) {
      return null;
    }
    appendRefWhr(scheme, refbillinfos, transtype);
    IWastAgeFetchBillService queryService = (IWastAgeFetchBillService)ServiceLocator.find(IWastAgeFetchBillService.class);
    try {
      vos = queryService.querySaleOutBill(scheme);
    } catch (BusinessException e) {
      ExceptionUtils.wrapException(e);
    } 
    
    if (null == vos) {
      return null;
    }
    BillBodySortUtils.sort(vos);
    BillGridOperator operator = new BillGridOperator(info.getTempletid(), info.getPageCode());
    BillGrid[] grids = new BillGrid[vos.length];
    for (int i = 0; i < vos.length; i++) {
      grids[i] = operator.toBillGrid(vos[i]);
    }
    processResult(grids, vos);
    return grids;
  }








  
  public void processResult(BillGrid[] cards, SaleOutVO[] vos) {
    for (int i = 0; i < cards.length; i++) {
      BillGrid billgrid = cards[i];
      
      ICBaseBillGridPrecisionHandler handler = new SaleOutBillGridPrecisionHandler(billgrid);
      handler.process();

      
      SaleOutVO vo = vos[i];
      for (int j = 0; j < vo.getBodys().length; j++) {
        SaleOutBodyVO body = vo.getBody(j);
        
        Double ncaninnum = getNcaninnum(body);
        
        Cell cell = new Cell();
        cell.setValue(ncaninnum);
        
        int scale = billgrid.getBody().getModel().getRows()[j].getCell("nnum").getScale();
        cell.setScale(scale);
        
        BigDecimal display = (new BigDecimal(ncaninnum.doubleValue())).setScale(scale, 4);
        cell.setDisplay(display.toString());
        billgrid.getBody().getModel().getRows()[j].addCell("ncaninnum", cell);
      } 
    } 
  }








  
  private Double getNcaninnum(SaleOutBodyVO body) {
    UFDouble UFnnum = body.getNnum();
    double nnum = (UFnnum != null) ? UFnnum.toDouble().doubleValue() : 0.0D;
    UFDouble UFnaccumoutsignnum = body.getNaccumoutsignnum();
    double naccumoutsignnum = (UFnaccumoutsignnum != null) ? UFnaccumoutsignnum.toDouble().doubleValue() : 0.0D;
    UFDouble UFnaccumoutbacknum = body.getNaccumoutbacknum();
    double naccumoutbacknum = (UFnaccumoutbacknum != null) ? UFnaccumoutbacknum.toDouble().doubleValue() : 0.0D;
    UFDouble UFnaccumwastnum = body.getNaccumwastnum();
    double naccumwastnum = (UFnaccumwastnum != null) ? UFnaccumwastnum.toDouble().doubleValue() : 0.0D;
    
    return Double.valueOf(nnum - naccumoutsignnum - naccumoutbacknum - naccumwastnum);
  }
  
  private PullBillInfoVO[] queryRefPara(Object transtype) {
    ClientInfo clientinfo = SessionContext.getInstance().getClientInfo();
    String groupId = clientinfo.getPk_group();
    String userId = clientinfo.getUserid();
    IPFBusinessTypeService pfservice = (IPFBusinessTypeService)ServiceLocator.find(IPFBusinessTypeService.class);
    PullBillInfoVO[] refbillinfos = null;
    try {
      refbillinfos = pfservice.getPullBillInfo(ICBillType.WastageBill.getCode(), (null == transtype) ? null : (String)transtype, groupId, userId);
    }
    catch (BusinessException e) {
      ExceptionUtils.wrapException(e);
    } 
    
    return refbillinfos;
  }
  
  private void appendRefWhr(IQueryScheme scheme, PullBillInfoVO[] refbillinfos, Object obj) {
    if (null == refbillinfos || refbillinfos.length == 0) {
      return;
    }
    List<String> busitypes = new ArrayList<String>();
    List<String> transtypes = new ArrayList<String>();
    for (PullBillInfoVO info : refbillinfos) {
      if ("4C".equals(info.getSrc_billtype())) {
        
        List<String> busitypes1 = info.getBusitypes();
        List<String> transtypes1 = info.getTranstypes();
        busitypes.addAll(busitypes1);
        if (!transtypes1.contains("")) {
          transtypes1.addAll(transtypes);
        }
      } 
    } 
    
    if (!ValueCheckUtil.isNullORZeroLength(busitypes) && !StringUtil.isSEmptyOrNull((String)busitypes.get(0))) {
      scheme.put("busitypearray", busitypes.toArray(new String[0]));
      if (!ValueCheckUtil.isNullORZeroLength(transtypes) && !StringUtil.isSEmptyOrNull((String)transtypes.get(0)))
        scheme.put("transtypearray", transtypes.toArray(new String[0])); 
    } 
  }
}